package com.kehao.leetcode.middle.math;

import org.junit.Test;

public class DivideSolution {

    //解题思路：https://leetcode-cn.com/problems/divide-two-integers/solution/jian-dan-yi-dong-javac-pythonjs-liang-sh-ptbw/
    public int divide(int dividend, int divisor) {
        int result = 0;
        boolean sign =  (dividend>0)^(divisor>0);
        if(dividend==Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE;
        dividend = Math.abs(dividend);
        divisor = Math.abs(divisor);
        for (int i = 31;i>=0 ;i--) {
            if((dividend>>>i)-divisor>=0){
                dividend -= divisor<<i;
                result += (1<<i);
            }
        }
        return sign?-result:result;
    }

    @Test
    public void test01(){
        System.out.println(divide(-10, 3));
    }
}
